package com.example.Lezedata.ui.activity.person;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.widget.TextView;

import com.example.Lezedata.R;
import com.example.Lezedata.dataManager.GBUserManager;
import com.example.Lezedata.net.GBError;
import com.example.Lezedata.net.GBNetRequest;
import com.example.Lezedata.net.ServerResponseObserver;
import com.example.Lezedata.ui.activity.baseClass.GBBaseVc;
import com.example.Lezedata.util.StatusBarUtil;

import java.math.BigDecimal;

import bid.Bid;

public class MeVc extends GBBaseVc {

    @Override
    protected int getLayoutId() {
        return R.layout.activity_me_vc;
    }

    @Override
    public CharSequence title() {
        return  "数据中心";
    }

    private TextView pooldata;
    private TextView pooltotal;
    private TextView mynode;
    private TextView mytotal;
    private TextView mynodep;
    private TextView mytotalp;
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void viewDidLoad() {
        super.viewDidLoad();
        StatusBarUtil.setStatusBarColor(this,R.color.app_color_main);
        StatusBarUtil.setLightStatusBar(this,false,false);
        titleTextView.setTextColor(Color.WHITE);
        toolbar.setBackground(getDrawable(R.drawable.top));

          pooldata = findViewByTag("pooldata");
          pooltotal = findViewByTag("pooltotal");
          mynode = findViewByTag("mynode");
          mytotal = findViewByTag("mytotal");
          mynodep = findViewByTag("mynodep");
          mytotalp = findViewByTag("mytotalp");
        loadData();
    }

    void loadData(){
        GBNetRequest.stubUser().getNodePoolProperty(Bid.GetNodePoolPropertyRequest.newBuilder().build(),
                ServerResponseObserver.observer(new ServerResponseObserver.requestListener<Bid.GetNodePoolPropertyResponse>() {
            @Override
            public void complete(final Bid.GetNodePoolPropertyResponse pool, GBError error) {
                if (error == null){
                    GBNetRequest.stubUser().getUserFinanceAccount(Bid.GetUserFinanceAccountRequest.newBuilder().setUserId(GBUserManager.getUserId()).build(),
                            ServerResponseObserver.observer(new ServerResponseObserver.requestListener<Bid.GetUserFinanceAccountResponse>() {
                                @Override
                                public void complete(Bid.GetUserFinanceAccountResponse value, GBError error) {
                                    if (error == null){
                                        refreshMyData(pool,value);
                                    }
                                }
                            })
                    );
                }
            }
        }));
    }



    @SuppressLint("DefaultLocale")
    void refreshMyData(Bid.GetNodePoolPropertyResponse pool, Bid.GetUserFinanceAccountResponse value){
        pooldata.setText(String.valueOf(pool.getPeerCount()));
        pooltotal.setText(String.valueOf(pool.getChequeCount()));

        mynode.setText(String.valueOf(value.getPeerCount()));
        mytotal.setText(String.valueOf(value.getChequeCount()));

        String v1 = String.format("%.6f",((float)value.getPeerCount())/((float)pool.getPeerCount()));
        String v2 = String.format("%.6f",((float)value.getChequeCount())/((float)pool.getChequeCount()));

        mynodep.setText(String.valueOf(v1));
        mytotalp.setText(String.valueOf(v2));
    }

    @Override
    protected boolean hideNavigationBack() {
        return true;
    }
}